Ingestion Techniques

How can you create Delta tables from CSV files using CTAS?


  • CREATE TABLE AS SELECT (CTAS) creates and populates a table in one operation using the results of a query.

  • Use read_files to read source files from a Unity Catalog volume and specify options such as format => 'csv', header => true, and inferSchema => true to interpret the input correctly.

  • Select only the required source columns rather than SELECT * to avoid including metadata columns such as _rescued_data in the destination table.

  • Example (replace the column names and volume path with your own):

    CREATE TABLE new_hires_ctas AS
    SELECT employee_id, first_name, last_name, role
    FROM read_files('/Volumes/my_catalog/my_schema/myfiles/employees2.csv',
                    format => 'csv', header => true, inferSchema => true);
  • CTAS is useful for repeatable, code-based ingestion workflows; it creates the table from the query result rather than appending to an existing table.

How can you ingest a CSV file without writing code?


  • In Catalog Explorer, navigate to the destination catalog and schema and use the table creation/upload option to upload a CSV file through the UI.
  • Review the detected columns and data types before creating the table.
  • The Upload UI is suited to quick, ad-hoc imports, while CTAS is better for workflows that need to be documented and reproduced.

How do you incrementally load files using COPY INTO?


  • Create the destination Delta table with an explicit schema before loading data; unlike CTAS, COPY INTO loads data into an existing table.

  • COPY INTO reads files from a specified location and loads files it has not previously processed into the destination table.

  • Example (adjust the schema, location and options to match the files):

    CREATE TABLE practice_copyinto (
      employee_id INT,
      first_name STRING,
      last_name STRING,
      role STRING
    );
    
    COPY INTO practice_copyinto
    FROM '/Volumes/my_catalog/my_schema/myfiles/'
    FILEFORMAT = CSV
    FORMAT_OPTIONS ('header' = 'true', 'inferSchema' = 'true');
  • COPY INTO tracks processed files and normally skips them on subsequent runs, making repeat execution idempotent. If no new files arrive, rerunning the same command does not add duplicate rows.

  • It is useful for scheduled incremental ingestion; however, the file-tracking behavior should not be treated as general-purpose row-level deduplication.

How can you verify ingested data and confirm incremental loading?


  • Run SHOW TABLES; to list tables in the current schema and confirm that the destination table exists.
  • Use SELECT COUNT(*) FROM table_name; to check the number of ingested rows.
  • Query information_schema.columns to inspect and compare column names and data types between tables.
  • Re-run the same COPY INTO statement and check that no additional rows are loaded from already-processed files.
  • Run DESCRIBE HISTORY table_name; to inspect Delta table operations and confirm which loads created new table versions.